const express = require('express')
const c = require('../connection')
const router = express.Router()
//通过车辆列表编号查询车辆接口
router.get('/car-query', (req, res) => {
    var obj = req.query
    c.query(`select * from uc_carlist where lid=?`, [obj.lid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '车辆信息查询',
                data: r
            })
        }
    })
})
//首页用的省份接口
router.get('/car-province', (req, res) => {
    var obj = req.query
    c.query(`select count(lid) as count from uc_carlist where provinceid=?`, [obj.provinceid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            total = r[0].count
            c.query(`select provinceid from uc_carlist where provinceid=?`, [obj.provinceid], (err, r) => {
                if (err) {
                    throw err
                }
                if (r.length > 0) {
                    name = r[0].provinceid
                    value = total
                    result = { name, value }
                    res.send({
                        code: 200,
                        msg: '省份数量查询成功',
                        data: r,
                        total: total,
                        result,
                    })
                } else {
                    res.send({
                        code: 401,
                        msg: '查询失败'
                    })
                }
            })
        }
    })
})
//首页用的品牌接口
router.get('/car-brand', (req, res) => {
    var obj = req.query
    c.query(`select count(lid) as count from uc_carlist where brand=?`, [obj.brand], (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            total = r[0].count
            c.query(`select brand from uc_carlist where brand=?`, [obj.brand], (err, r) => {
                if (err) {
                    throw err
                }
                if (r.length > 0) {
                    console.log(r);
                    name = r[0].brand
                    value = total
                    result = { name, value }
                    res.send({
                        code: 200,
                        msg: '查询总数成功',
                        data: r,
                        total: total,
                        result: result,
                    })
                } else {
                    res.send({
                        code: 401,
                        msg: '查询失败'
                    })
                }
            })
        }
    })
})
//新增车辆接口
router.post('/car-add', (req, res) => {
    var obj = req.body
    c.query(`insert into uc_carlist set ?`, [obj], (err, r) => {
        if (err) {
            throw err
        }
        res.send({
            code: 200,
            msg: '新增车辆成功',
            data: r
        })
    })
})
//车辆列表通过车辆品牌进行分页查询接口
router.get('/car-list', (req, res) => {
    var obj = req.query
    pageNum = obj.pageNum * 1 || 1
    pageCount = 3
    start = (pageNum - 1) * pageCount
    obj.brand == 'undefined' ? '' : obj.brand
    c.query(`select count(lid) as count from uc_carlist where brand like ? order by ndate desc`, [`%${obj.brand}%`], (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            var total = r[0].count
            c.query(`select * from uc_carlist where brand like ? order by ndate desc limit ?,?`, [`%${obj.brand}%`, start * 1, pageCount * 1], (err, r) => {
                if (err) {
                    throw err
                }
                if (r.length > 0) {
                    res.send({
                        code: 200,
                        msg: '查询成功',
                        data: r,
                        total: total,
                        pageCount: pageCount,
                        pageNum: pageNum
                    })
                } else {
                    res.send({
                        code: 401,
                        msg: '查询失败',
                        data: r
                    })
                }
            })
        }
    })
})
//首页用的车辆列表接口
router.get('/car-list-index', (req, res) => {
    var obj = req.query
    pageNum = obj.pageNum * 1 || 1
    pageCount = 10
    start = (pageNum - 1) * pageCount
    obj.brand == 'undefined' ? '' : obj.brand
    c.query(`select count(lid) as count from uc_carlist `, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            var total = r[0].count
            c.query(`select * from uc_carlist limit ?,?`, [start * 1, pageCount * 1], (err, r) => {
                if (err) {
                    throw err
                }
                if (r.length > 0) {
                    res.send({
                        code: 200,
                        msg: '查询成功',
                        data: r,
                        total: total,
                        pageCount: pageCount,
                        pageNum: pageNum
                    })
                } else {
                    res.send({
                        code: 401,
                        msg: '查询失败',
                        data: r
                    })
                }
            })
        }
    })
})
//删除车辆信息接口
router.post('/car-delete', (req, res) => {
    var obj = req.body
    c.query(`delete from uc_carlist where lid=?`, [obj.lid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.affectedRows === 1) {
            res.send({
                code: 200,
                msg: '删除成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '删除失败',
                data: r
            })
        }
    })
})
router.post('/update', (req, res) => {
    var obj = req.body
    c.query(`update uc_carlist set ? where lid=?`, [obj, obj.lid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows === 0) {
            res.send({
                code: 401,
                msg: '修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        }
    })
})
module.exports = router